<?php
namespace app\api\Model;
use \think\Model;

class User extends Model {
	/**
	 * 验证用户
	 * @param  [type] $username [description]
	 * @param  [type] $password [description]
	 * @return [type]           [description]
	 */
	public function valide($post)
	{
		$user = new self;
		$result = $user->where(['username' => $post['username']])->select()->toArray();

		if(!$result) {
			return false;
		}
		if($result[0]['password'] != $post['password']) {
			return flase;
		}
		if((time() - $result[0]['token_createtime']) > 1) {
			$createToken = $this->createToken($post);
			if($createToken) {
				return $createToken;
			} else {
				return false;
			}
		}
		return true;
	}
	/**
	 * 生成tocken
	 * 
	 * @param  [type] $post [description]
	 * @return [type]       [description]
	 */
	public function createToken($post)
	{
		$data['access_token'] = md5($post['appid'] . $post['username'] . time());
		$data['token_createtime'] = time();
		$data['ip'] = $_SERVER['REMOTE_ADDR'];
		$user = new User();
		$result = $user->where(['username' => $post['username']])->update($data);
		if($result) {
			return $data['access_token'];
		} 
		return false;
	}
}